package com.jietong.rfid.ui.tag_operation;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import com.jietong.rfid.ui.R;
import com.jietong.rfid.uhf.service.R2000ReaderService;
import com.jietong.rfid.uhf.service.impl.R2000ReaderServiceImpl;
import com.jietong.rfid.ui.util.ReaderUtil;
import com.jietong.rfid.ui.util.Toasts;

public class IOOperationActivity extends Activity implements OnClickListener {
	private Button btnRead;
	private Button btnSet;
	private CheckBox cbInputPort1;
	private CheckBox cbInputPort2;
	private CheckBox cbOutputPort1;
	private CheckBox cbOutputPort2;
	private int workPattern;
	R2000ReaderService readerService = new R2000ReaderServiceImpl();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_params_set_input_output_operation);
		inital();
	}

	private void inital() {
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		cbInputPort1 = (CheckBox) findViewById(R.id.cb_input_port1);
		cbInputPort2 = (CheckBox) findViewById(R.id.cb_input_port2);
		
		cbOutputPort1 = (CheckBox) findViewById(R.id.cb_output_port1);
		cbOutputPort2 = (CheckBox) findViewById(R.id.cb_output_port2);
		
		btnRead = (Button) findViewById(R.id.btn_io_operation_read);
		btnSet = (Button) findViewById(R.id.btn_io_operation_set);

		btnRead.setOnClickListener(this);
		btnSet.setOnClickListener(this);
		
		btnRead.performClick();
	}

	/*****************************************************************************/
	public void shake_activity_back(View v) { // ������ ���ذ�ť
		this.finish();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_io_operation_read:
			ioOperationRead();
			break;
		case R.id.btn_io_operation_set:
			ioOperationSet();
			break;
		}
	}

	private void ioOperationSet() {
		try {
			int count = 0;
			int output1 = 0;
			int output2 = 0;
			
			output1 = cbOutputPort1.isChecked() == true ? 1 : 0;
			output2 = cbOutputPort2.isChecked() == true ? 1 : 0;
			
			boolean result = readerService.setGPIO(ReaderUtil.readers,(byte)1,(byte)output1);
			if(result){
				count++;
			}
			result = readerService.setGPIO(ReaderUtil.readers,(byte)2,(byte)output2);
			if(result){
				count++;
			}
			if(count == 2){
				Toasts.makeTextShort(this, R.string.msg_input_output_operation_set_succeed);
			}else{
				Toasts.makeTextShort(this,R.string.msg_input_output_operation_set_failed);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void ioOperationRead() {
		readerService.getGPIO(ReaderUtil.readers);
//		if(result > -1 && result < 3){
//			Toasts.makeTextShort(this,R.string.msg_input_output_operation_get_succeed);
//		}else{
//			Toasts.makeTextShort(this, R.string.msg_input_output_operation_get_failed);
//		}
	}
}